package com.shse.sale.service.impl;

import com.shse.sale.dao.ContentDao;
import com.shse.sale.dao.TrxDao;
import com.shse.sale.meta.Content;
import com.shse.sale.meta.Trx;
import com.shse.sale.meta.User;
import com.shse.sale.service.ContentService;
import com.shse.sale.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by shse on 3/19/2016.
 * 商品service实现
 */
@Service("contentService")
public class ContentServiceImpl implements ContentService {

    @Autowired
    private ContentDao contentDao;
    @Autowired
    private TrxDao trxDao;

    /**
     * 获取所有商品（分页后）
     */
    @Override
    public void getContents(HttpServletRequest request,
                            ModelMap map,
                            User user) {
        String pageNow = request.getParameter("pageNow");

        Page page;

        List<Content> contents;

        int totalCount = contentDao.getContentsCount();

        if (pageNow != null) {
            page = new Page(totalCount, Integer.parseInt(pageNow));
            contents = contentDao.getContentsByPage(page.getStartPos(), page.getPageSize());
        } else {
            page = new Page(totalCount, 1);
            contents = contentDao.getContentsByPage(page.getStartPos(), page.getPageSize());
        }

        //下面加入额外信息
        List<Trx> trxList = trxDao.getTrxs();
        for (Trx trx : trxList) {
            for (Content content : contents) {
                if (content.getId() == trx.getContentId()) {
                    content.setIsSell(true);
                    if(user!=null && trx.getPersonId() == user.getId()){
                        content.setIsBuy(true);
                    }
                }
            }
        }

//        for(Content content:contents){
//            Trx trx = trxDao.getTrxByContent(content.getId());
//            if(trx!=null){
//                content.setIsSell(true);
//                if(user!=null && user.getId() == trx.getPersonId()){
//                    content.setIsBuy(true);
//                }
//            }
//        }

        map.addAttribute("contentList", contents);
        map.addAttribute("page", page);

    }


    /**
     * 获取传入用户未购买商品，带分页
     * @param request 当前请求
     * @param map ModelMap
     * @param user 当前用户
     */
    @Override
    public void getUnBuyContents(HttpServletRequest request, ModelMap map, User user) {
        String pageNow = request.getParameter("pageNow");

        Page page;
        List<Content> contentList;

        int totalCount = contentDao.getUnBuyContentsCount(user.getId());

        if (pageNow != null) {
            page = new Page(totalCount, Integer.parseInt(pageNow));
            contentList = contentDao.getUnBuyContentsByPage(page.getStartPos(), page.getPageSize(), user.getId());
        } else {
            page = new Page(totalCount, 1);
            contentList = contentDao.getUnBuyContentsByPage(page.getStartPos(), page.getPageSize(), user.getId());
        }

        map.addAttribute("contentList", contentList);
        map.addAttribute("page", page);

    }

    /**
     * 获取指定用户购买的商品
     *
     * @param user 当前用户
     * @return 用户购买的商品
     */
    @Override
    public List<Content> getBuyedContents(User user) {

        List<Content> contentList = contentDao.getBuyedContents(user.getId());
        return contentList;
    }

    /**
     * 通过商品id获取商品
     *
     * @param id 商品id
     * @return 商品对象
     */
    @Override
    public Content getContentById(String id) {
        Content content = contentDao.getContentById(Integer.parseInt(id));
        Trx trx = trxDao.getTrxByContent(Integer.parseInt(id));
        if (trx != null) {
            content.setBuyPrice(trx.getPrice());
            content.setIsBuy(true);
        }
        return content;
    }


    /**
     * 添加商品，部分消息可作为之后扩充前端功能使用
     *
     * @param content 商品对象
     * @return 发布商品的消息
     */
    @Override
    public String addContent(Content content) {
        if (content.getTitle().length() < 2 || content.getTitle().length() > 80) {
            return "标题长度应在[2,80]字符内";
        }
        if (content.getSummary().length() < 2 || content.getSummary().length() > 140) {
            return "摘要长度应在[2,140]字符内";
        }
        if (content.getDetail().length() < 2 || content.getDetail().length() > 1000) {
            return "正文长度应在[2,1000]字符内";
        }
        contentDao.addContent(content);
        return "正常发布";
    }

    /**
     * 编辑商品，部分消息可作为之后扩充前端功能使用
     *
     * @param content 商品对象
     * @return 编辑商品的消息
     */
    @Override
    public String updateContent(Content content) {
        if (content.getTitle().length() < 2 || content.getTitle().length() > 80) {
            return "标题长度应在[2,80]字符内";
        }
        if (content.getSummary().length() < 2 || content.getSummary().length() > 140) {
            return "摘要长度应在[2,140]字符内";
        }
        if (content.getDetail().length() < 2 || content.getDetail().length() > 1000) {
            return "正文长度应在[2,1000]字符内";
        }
        contentDao.updateContent(content);
        return "正常更新";
    }

    /**
     * 通过商品id删除对应商品
     *
     * @param id 商品id
     */
    @Override
    public void deleteContent(String id) {
        contentDao.deleteContent(id);
    }


}
